package com.minimalist.common.config.trace;

import com.minimalist.common.config.interceptor.DynamicRateLimitInterceptor;
import com.minimalist.common.config.interceptor.IpBasedRateLimitInterceptor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.format.DateTimeFormatter;

/**
 * @author 11987
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

    @Autowired
    private TraceIdInterceptor traceIdInterceptor;

    @Autowired
    private IpBasedRateLimitInterceptor ipBasedRateLimitInterceptor;

    @Autowired
    private DynamicRateLimitInterceptor dynamicRateLimitInterceptor;

    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry){
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(traceIdInterceptor);
        registry.addInterceptor(ipBasedRateLimitInterceptor);
        registry.addInterceptor(dynamicRateLimitInterceptor);
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        registrar.registerFormatters(registry);
    }

}
